﻿#ifndef __SS_RPC_MGR_H__
#define __SS_RPC_MGR_H__
#include <set>
#include <map>
#include <list>
#include <functional>
//#include <memory>
#include "../tools/ssIDAlloter.h"
#include "../ssBaseDefine.h"

namespace StarSeeker
{

class ssRPCMgr
{
public:
	enum class eError
	{
		OK=0,
		TIME_OUT,
		SN_REPEAT
	};
	typedef std::function<void(ssRPCMgr::eError, ulint param1, ulint param2, ulint param3)> RpcCallback;
	//typedef std::shared_ptr<RpcCallback> callbackPtr;
private:
	struct CbFunc
	{
		i64 timeOut;
		u32 sn;
		RpcCallback func;
	};
	//typedef std::shared_ptr<CbFunc> CbFuncPtr;
public:
	ssRPCMgr();
	~ssRPCMgr();

	bool IsRegistered(u32 cbMsgId);
	void RegisterCallback(u32 sn, const RpcCallback& doFunc, u32 cbMsgId = 0, i64 nowMs=0);
	u32 AutoRegister(const RpcCallback& doFunc, u32 cbMsgId =0, i64 nowMs=0);
	void Update(i64 nowMs);
	void TriggerCallback(u32 sn, ulint param1, ulint param2, ulint param3);

public:
	std::set<u32>					m_allMsgID;//所有协议ID
	i32								m_timeOutMs;//超时时间（毫秒）
protected:
	std::list<std::pair<u32,i64> >	m_cbList;//回调队列<sn,timeout>
	std::map<u32, CbFunc>			m_cbMap;
	ssCumulationID<u32>				m_sn;
};

}
#endif
